package com.dao.rxjava01.uis.activities;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import com.dao.green.cn.applibary.uis.BaseActivity;
import com.dao.rxjava01.R;
import com.dao.rxjava01.databinding.AcIndexLayoutBinding;
import com.dao.rxjava01.databinding.ItemIndexListviewBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ant on 2017/7/10  12:05
 * <p>
 * <p>
 *
 * @version 1.0.0
 * @auther Ant
 */

public class IndexActivity extends BaseActivity {
    private AcIndexLayoutBinding binding;
    private MyAdapter adapter;

    private List<String> strList;
    private List<Class<? extends BaseActivity>> clsList;

    @Override
    public void bindLayout() {
        strList = new ArrayList<>();
        clsList = new ArrayList<>();
        binding = DataBindingUtil.setContentView(this, R.layout.ac_index_layout);
    }

    @Override
    public void initView() {
        adapter = new MyAdapter(this);
        binding.acIndexListView.setAdapter(adapter);

    }

    @Override
    public void initData() {
        strList.add("MainActivity");
        clsList.add(MainActivity.class);
        strList.add("Demo01");
        clsList.add(Demo01.class);
        strList.add("Demo02");
        clsList.add(Demo02.class);

        strList.add("demo03");
        clsList.add(Demo03.class);

        adapter.setData(strList);
    }

    @Override
    public void initListener() {
        binding.acIndexListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(IndexActivity.this, clsList.get(position)));
            }
        });
    }


    public class IndexPresener {

    }


    public class MyAdapter extends BaseAdapter {
        private Context context;
        private List<String> data;
        private LayoutInflater inflater;
        private ItemIndexListviewBinding itemBinding;


        public MyAdapter(Context context) {
            this.context = context;
            this.inflater = LayoutInflater.from(context);
        }


        public void setData(List<String> data) {
            this.data = data;
            this.notifyDataSetChanged();
        }


        @Override
        public int getCount() {
            return data == null ? 0 : data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {

                itemBinding = DataBindingUtil.inflate(inflater, R.layout.item_index_listview, parent, false);
                convertView = itemBinding.getRoot();


                convertView.setTag(itemBinding);
            } else {
                itemBinding = (ItemIndexListviewBinding) convertView.getTag();
            }
            // binding data
            itemBinding.setText(data.get(position));

            return convertView;
        }
    }

}
